@extends('layouts.admin')
@section('content')
    <script type="text/javascript" src="{{asset('resources/org/zTree/js/jquery.ztree.core.min.js')}}"></script>
    <script type="text/javascript" src="{{asset('resources/org/zTree/js/jquery.ztree.excheck.min.js')}}"></script>
    <link rel="stylesheet" href="{{asset('resources/org/zTree/css/zTreeStyle/zTreeStyle.css')}}" type="text/css">
    <!--面包屑导航 开始-->
    <div class="crumb_warp">
        <!--<i class="fa fa-bell"></i> 欢迎使用登陆网站后台，建站的首选工具。-->
        <i class="fa fa-home"></i> <a href="{{url('admin/info')}}">首页</a> &raquo; <a href="{{url('admin/group')}}">用户组管理</a> &raquo; 创建分组
    </div>
    <!--面包屑导航 结束-->

	<!--结果集标题与导航组件 开始-->
	<div class="result_wrap">
        <div class="result_title">
            <h3><a href="{{url('admin/group')}}"><i class="fa fa-reply"></i>返回</a></h3>
            @if(count($errors)>0)
                <div class="mark">
                    <p>{{$errors}}</p>
                </div>
            @endif
        </div>
    </div>
    <!--结果集标题与导航组件 结束-->
    
    <div class="result_wrap">
        {{csrf_field()}}
        <table class="add_tab">
            <tbody>
                <tr>
                    <th><i class="require">*</i>分组名称：</th>
                    <td>
                        <input type="text" class="lg" name="name">
                    </td>
                </tr>

                <tr>
                    <th>默认权限：</th>
                    <td id="chose">
                        <ul id="treeAll" class="ztree"></ul>
                    </td>
                </tr>
                <tr>
                    <th></th>
                    <td>
                        <input type="submit" onclick="onCheck()" value="提交">
                        <input type="button" class="back" onclick="history.go(-1)" value="返回">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
    <script type="text/javascript">
        <!--
        var setting = {
            check: {
                enable: true,
                chkboxType:{ "Y" : "ps", "N" : "s" }
            },
            data: {
                simpleData: {
                    enable: true
                }
            }
        };
        var zNodes=JSON.parse('<?php echo json_encode($role); ?>');

        var code;
        $(document).ready(function(){
            $.fn.zTree.init($("#treeAll"), setting, zNodes);
        });
        //-->
        function onCheck(e,treeId,treeNode){
            var treeObj=$.fn.zTree.getZTreeObj("treeAll"),
                    nodes=treeObj.getCheckedNodes(true);
            var arr={};
            for(var i=0;i<nodes.length;i++){
                arr[nodes[i].role]=1;
            }
            //console.log(nodes);
            var jarr=JSON.stringify(arr);
            var json={'_token':"{{csrf_token()}}",'name':$("input[name=name]").val(),'permission':jarr};
            $.post("{{url('admin/group/createGroup/')}}",json,function (data) {
                if(data.status==200){
                    layer.msg(data.msg, {icon: 6});
                }else{
                    layer.msg(data.msg, {icon: 5});
                }
            });
        }
    </script>
@endsection